
<?php
// PHP Calendar (version 2.3), written by Keith Devens
// http://keithdevens.com/software/php_calendar
//  see example at http://keithdevens.com/weblog
// License: http://keithdevens.com/software/license
// dam si aktualny den nejak vyrazne
require_once( dirname(__FILE__).'/../libs/bedmintonlib.php' );

$curYear = 0;
$curMonth = 0;
$curDay = 0;
$selectedMonth = 0;

function generate_calendar($year, $month, $day_name_length = 3, $first_day = 0)
{
    global $curYear, $curMonth, $curDay, $selectedMonth;
    $english = isEnglish();
   // $days = array($today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger; text-decoration: blink;">'.$today.'</span>')); 
    $first_of_month = gmmktime(0,0,0,$month,1,$year);
    #remember that mktime will automatically correct if invalid dates are entered
    # for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
    # this provides a built in "rounding" feature to generate_calendar()

    $day_names = array(); #generate all the day names according to the current locale
    if ($english)
    {
        $day_names[ 0 ] = 'Monday';
        $day_names[ 1 ] = 'Tuesday';
        $day_names[ 2 ] = 'Wednesday';
        $day_names[ 3 ] = 'Thursday';
        $day_names[ 4 ] = 'Friday';
        $day_names[ 5 ] = 'Saturday';
        $day_names[ 6 ] = 'Sunday';
    }
    else
    {
        $day_names[ 0 ] = 'Pondelok';
        $day_names[ 1 ] = 'Utorok';
        $day_names[ 2 ] = 'Streda';
        $day_names[ 3 ] = 'Štvrtok';
        $day_names[ 4 ] = 'Piatok';
        $day_names[ 5 ] = 'Sobota';
        $day_names[ 6 ] = 'Nedeľa';
    }

    $h = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
    list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
    $weekday = ($weekday + 7 - $first_day - 1) % 7; #adjust for $first_day lebo oni maju nedelu prvu
    $title   = htmlentities(ucfirst($month_name)).'&nbsp;'.$year;  #note that some locales don't capitalize month and day names

    $calendar = "<table class=\"calendar\">";
    if($day_name_length)
    { 
        #if day_name_length is >3, the full name of the day will be printed
        $calendar .= "<tr>";
        foreach($day_names as $d)
        {
            $calendar .= '<th>'. ( $day_name_length < 4 ? mb_substr($d,0,$day_name_length,'UTF-8') : $d ).'</th>';
        }
        $calendar .= "</tr>\n<tr>";
    }

    if($weekday > 0) 
        $calendar .= '<td colspan="'.$weekday.'">&nbsp;</td>'; #initial 'empty' days
    
    for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++)
    {
        if($weekday == 7)
        {
            $weekday   = 0; #start a new week
            $calendar .= "</tr>\n<tr>";
        }
       
        // zistim si aktualny den a ten nejako vysvietim
        $hrefStr = "";
        if ($curMonth == $selectedMonth && $day < $curDay)
            $hrefStr = sprintf("<span class=\"daynohref\">%d</span>", $day );
        else
            $hrefStr = sprintf("<a id =\"day%s\" class=\"day\" href=\"javascript:void(0)\">%d</a>", $day, $day );

        $calendar .= "<td>$hrefStr</td>";
    }
    if($weekday != 7) 
        $calendar .= '<td colspan="'.(7-$weekday).'">&nbsp;</td>'; #remaining "empty" days

    return $calendar."</tr>\n</table><p>\n";
}

$now = getdate();
$curYear = $now['year'];
$curMonth = $now['mon'];
$curDay = $now['mday'];
$selectedMonth = 0;

if (isset( $_GET[ 'selectedMonth' ] ))
{
    $selectedMonth = $_GET[ 'selectedMonth' ];
    if ($curMonth > $selectedMonth)
        $curYear ++;
}

echo generate_calendar( $curYear, $selectedMonth, 3 );

$selDay = $curDay;
if ($curMonth != $selectedMonth)
    $selDay = 1;

if (isset( $_GET[ 'selectedDay' ] ))
    $selDay = $_GET[ 'selectedDay' ];

$javascriptStr = "
<script type=\"text/javascript\"> 
    
    var currentDay = $selDay;
    var previousDay = currentDay;

    $(\".day\").click(function()
    {
       var dateStr = '$curYear-$selectedMonth-';
       dayStr = $(this).attr( 'id' );
       currentDay = dayStr.substring( 3 );
       dateStr += currentDay;
       var urlStr = '../ajax/reservationdate.php?reservationDate=' + dateStr;

       var selectorStr = '#day' + currentDay;
       $(selectorStr).addClass('selectedDay').removeClass('day');
       if (currentDay != previousDay)
       {
           selectorStr = '#day' + previousDay;
           $(selectorStr).addClass('day').removeClass('selectedDay');
       }

       previousDay = currentDay;
       $(\"#reservationTable\").load( urlStr );  
    });

    selectorStr = '#day' + currentDay;
    $(selectorStr).click();

    </script>

    <div id = \"reservationTable\"></div>";

echo( $javascriptStr );

?>